/**
 * Copyright (c) 2019 xstgongyi.org
 * All rights reserved.
 */
package org.xstgongyi.eduaid.controller;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xstgongyi.eduaid.common.ControllerHelper;
import org.xstgongyi.eduaid.dataobject.ProjectPhaseDO;
import org.xstgongyi.eduaid.dataobject.ProjectDO;
import org.xstgongyi.eduaid.dataobject.SemesterDO;
import org.xstgongyi.eduaid.service.ProjectService;
import org.xstgongyi.eduaid.service.SemesterService;
import org.xstgongyi.eduaid.service.ProjectPhaseService;

/**
 * @author oldcrane<br/>
 * @created 2019-04-03
 */
@Controller
@RequestMapping("/projects")
public class ProjectController {

	@Autowired
	private ProjectService projectService;

	@Autowired
	private ProjectPhaseService projectPhaseService;

	@Autowired
	SemesterService semesterService;

	@GetMapping("/entry")
	public String entryPage(ModelMap map) {
		return "masterdata/project_list";
	}

	@GetMapping("/detail")
	public String detailPage(@RequestParam(name = "id", defaultValue = "0") Integer projectId, ModelMap map) {

		ProjectDO project = projectService.getById(projectId);
		map.put("project", project);

		List<ProjectPhaseDO> phaseDOList = projectPhaseService.listByProject(projectId);
		List<Map<String, Object>> voList = new ArrayList<>();
		for (ProjectPhaseDO item : phaseDOList) {
			Map<String, Object> row = new HashMap<>();
			row.put("semesterName", semesterService.getById(item.getSemesterId()).getName());
			row.put("status", item.getStatus());
			voList.add(row);
		}
		map.put("projectPhaseList", voList);

		return "masterdata/project_detail";
	}

	@GetMapping("")
	@ResponseBody
	public Map<String, Object> list(@RequestParam(name = "active", required = false) Boolean active) {
		List<ProjectDO> projectDOList = null;
		if (null == active) {
			projectDOList = projectService.listAll();
		} else {
			// TODO 尚未决定active字段是否会设置，先返回全部 oldcrane 2019-04-03
			projectDOList = projectService.listAll();
		}

		List<ProjectVO> voList = new ArrayList<>();
		for (ProjectDO projectDO : projectDOList) {
			ProjectVO vo = ProjectVO.of(projectDO);
			voList.add(vo);
		}
		return ControllerHelper.successResponse(voList);
	}

	@PostMapping("")
	@ResponseBody
	public Map<String, Object> add(@RequestBody ProjectVO vo) {
		if (null == vo) {
			return (ControllerHelper.errorResponse(1, "非法输入：要添加的项目参数为空对象。"));
		}
		LocalDate startDate = LocalDate.parse(vo.getStartDate(), DateTimeFormatter.ISO_LOCAL_DATE);
		projectService.insert(vo.getName(), vo.getPrefix(), startDate, vo.getDescription());
		return ControllerHelper.successResponse(null);
	}

	@PostMapping("/{id}")
	@ResponseBody
	public Map<String, Object> update(@PathVariable("id") Integer id, @RequestBody ProjectVO vo) {
		if (null == vo) {
			return (ControllerHelper.errorResponse(1, "非法输入：要修改的项目参数为空对象。"));
		}
		LocalDate startDate = LocalDate.parse(vo.getStartDate(), DateTimeFormatter.ISO_LOCAL_DATE);
		projectService.update(id, vo.getName(), vo.getPrefix(), startDate, vo.getDescription());
		return ControllerHelper.successResponse(null);
	}

	@GetMapping("/{id}/phases")
	@ResponseBody
	public Map<String, Object> listProjectPhases(@PathVariable("id") Integer projectId,
			@RequestParam(name = "filter", required = false) String filter) {
		if (null != filter && "uncreated".equalsIgnoreCase(filter)) {
			List<SemesterDO> semesters = projectPhaseService.listUncreatedSemesters(projectId);
			return ControllerHelper.successResponse(semesters);
		} else {
			List<ProjectPhaseDO> phases = projectPhaseService.listByProject(projectId);
			List<SemesterDO> semesters = new ArrayList<>();
			for (ProjectPhaseDO item : phases) {
				SemesterDO semester = semesterService.getById(item.getSemesterId());
				semesters.add(semester);
			}
			return ControllerHelper.successResponse(semesters);
		}
	}

}
